/*
 *  Copyright (c) 2013 The pim project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef PR_SYSTEM_WRAPPERS_SOURCE_TIMER_POSIX_H_
#define PR_SYSTEM_WRAPPERS_SOURCE_TIMER_POSIX_H_

#include <assert.h>
#include <signal.h>
#include <time.h>

#include <map>

#include "../typedefs.h"
#include "critical_section_wrapper.h"
#include "timer_wrapper.h"

namespace qb {

struct TimerItem {
  int32_t timerID;
  timer_t event_timerID;
  void* timer;
  void* userData;
  LPTIMERCALLBACK callback;
};

typedef std::map<uint32_t, TimerItem*> TimerList;
typedef TimerList::iterator TimerListItr;

class TimerPosix : public TimerWrapper {
 public:
  TimerPosix(void);

  virtual ~TimerPosix(void);

  virtual int32_t SetTimer(uint32_t periodicMS, LPTIMERCALLBACK callback,
                           void* userData);

  virtual bool KillTimer(int32_t timerID);

  virtual bool IsTimer(int32_t timerID);

 private:
  void OnTimer(TimerItem* item);
  static void __TimeProc(int sig, siginfo_t* si, void* uc);

 private:
  TimerList timerList_;
  CriticalSectionWrapper& crit_cs_;
};

};  // namespace qb

#endif  // PR_SYSTEM_WRAPPERS_SOURCE_TIMER_POSIX_H_
